package barinadroid.game;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.text.Text;
import org.andengine.util.color.Color;

public class SettingsScene extends MenuScene implements IOnMenuItemClickListener
{
	public static SettingsScene instance;

	public static SettingsScene getSharedInstace()
	{
		if(instance == null)
			instance = new SettingsScene();
		return instance;
	}

	private Scene callerScene;
	private Text controlsScaleText;
	public float controlsScale;
	private final int MENU_PLUS = 0, MENU_MINUS = 1;

	private SettingsScene()
	{
		super(GameActivity.getSharedInstance().camera);
		setBackground(new Background(Color.RED));
		controlsScale = 1;
		refreshItems();
		SpriteMenuItem plusItem = new SpriteMenuItem(MENU_PLUS, GameActivity.getSharedInstance().mOnScreenButtonATextureRegion, GameActivity.getSharedInstance()
				.getVertexBufferObjectManager());
		plusItem.setPosition(50, controlsScaleText.getX() + controlsScaleText.getHeight() + 20);
		SpriteMenuItem minusItem = new SpriteMenuItem(MENU_MINUS, GameActivity.getSharedInstance().mOnScreenButtonBTextureRegion, GameActivity.getSharedInstance()
				.getVertexBufferObjectManager());
		minusItem.setPosition(plusItem.getX() + plusItem.getWidth() + 20, controlsScaleText.getX() + controlsScaleText.getHeight() + 20);
		// registerTouchArea(plusItem);
		// registerTouchArea(minusItem);
		addMenuItem(plusItem);
		addMenuItem(minusItem);
		setOnMenuItemClickListener(this);
	}

	public void setCallerScene(Scene callerScene)
	{
		this.callerScene = callerScene;
	}

	public Scene getCallerScene()
	{
		return this.callerScene;
	}

	@Override
	public boolean onMenuItemClicked(MenuScene arg0, IMenuItem arg1, float arg2, float arg3)
	{
		switch (arg1.getID())
		{
			case MENU_PLUS:
				this.controlsScale += 0.1f;
				break;
			case MENU_MINUS:
				this.controlsScale -= 0.1f;
				break;
			default:
				return false;
		}
		refreshItems();
		return true;
	}

	private void refreshItems()
	{
		if(controlsScaleText != null)
		{
			detachChild(controlsScaleText);
			controlsScaleText.dispose();
		}
		controlsScaleText = new Text(50, 20, GameActivity.getSharedInstance().font, GameActivity.getSharedInstance().getString(R.string.controlsScale) + controlsScale,
				GameActivity.getSharedInstance().getVertexBufferObjectManager());
		attachChild(controlsScaleText);
	}
}